package me.firstapp.service.weixin.account.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.utils.Page;
import me.firstapp.module.weixin.jinbi.JinBiAccount;
import me.firstapp.module.weixin.jinbi.JinBiAccountTrace;
import me.firstapp.module.weixin.member.Member;
import me.firstapp.repository.weixin.jinbi.JinBiAccountRepository;
import me.firstapp.repository.weixin.jinbi.JinBiAccountTraceRepository;
import me.firstapp.repository.weixin.member.MemberRepository;
import me.firstapp.service.weixin.account.JinBiAccountTraceService;

@Service
public class JinBiAccountTraceServiceImpl implements JinBiAccountTraceService {

	@Autowired
	private JinBiAccountTraceRepository jinBiAccountTraceRepository;

	@Autowired
	private MemberRepository memberRepository;

	@Autowired
	private JinBiAccountRepository jinBiAccountRepository;

	@Override
	public List<JinBiAccountTrace> findJinBiAccountTracePageByOpenId(Long jinBiAccountId, int pageIndex, int pageSize) {
		return jinBiAccountTraceRepository.findJinBiAccountTracePageByOpenId(jinBiAccountId, pageIndex, pageSize);
	}

	@Override
	public Page<JinBiAccountTrace> findJinBiAccountTracePage(String openId, Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Date startTime, Date endTime) {
		Member member = memberRepository.findMemberByOpenId(openId);
		JinBiAccount jinBiAccount = jinBiAccountRepository.findJinBiAccountByCardNoAndOpenId(member.getCardNo(),
				openId);
		if (null != jinBiAccount) {
			return jinBiAccountTraceRepository.findJinBiAccountTracePage(jinBiAccount.getId(), pageNo, pageSize,
					searchParams, sortParam, sortType, startTime, endTime);
		} else {
			return new Page<>(1, 1);
		}
	}

	@Override
	public JinBiAccountTrace findById(Long id) {
		return (JinBiAccountTrace) jinBiAccountTraceRepository.getById(id);
	}

}
